package pzl.com.drawrelated.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import pzl.com.drawrelated.Utils.Pog;
import pzl.com.drawrelated.R;

/**
 * @author zl.peng
 * @version [1.0, 2016-11-17]
 */
public class V1 extends View {
    Bitmap b1, b2;
    int h;

    public V1(Context context) {
        super(context);
    }

    public V1(Context context, AttributeSet attrs) {
        super(context, attrs);
        b1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
        b2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.test2);
        h = b1.getHeight();
        Pog.i("h=" + h);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ViewGroup.LayoutParams lp = getLayoutParams();
        lp.height = h;
        requestLayout();
        Pog.i("lp.height=" + lp.height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint p = new Paint();
        p.setColor(Color.LTGRAY);
        p.setStrokeWidth(12);
        p.setStyle(Paint.Style.STROKE);
        p.setAntiAlias(true);


        Path path = new Path();
        path.addCircle(getWidth() / 2, getHeight() / 2, 120, Path.Direction.CCW);
        canvas.save();
        canvas.clipPath(path);


        canvas.drawBitmap(b1, 0, 0, new Paint());
        canvas.restore();

        path.reset();
        path.addRect(0, getHeight() / 2 + 60, getWidth(), getHeight() / 2 + 180, Path.Direction.CCW);
        canvas.save();
        canvas.clipPath(path);

        canvas.drawBitmap(b2, 0, 0, new Paint());
        canvas.restore();

        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 120, p);

    }

}
